package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.CencesObs;
import com.lebang.domain.TaskRouse;
import com.lebang.example.CencesObsExample;
import com.lebang.model.MyConfig;
import com.lebang.service.NewRouseService;
import com.lebang.service.cences.CencesObsService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;


/**
 * cences_obs
 *
 * @author hwq
 * @date 2020-07-14
 */
@Controller
@RequestMapping("/sys/cencesobs")
public class CencesObsController {
    @Autowired
    private CencesObsService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @Login
    @GetMapping("/list")
    public String list(Model model, HttpSession session)     {
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/cencus/cencesobs/list";
    }

    @Login
    @GetMapping("/add")
    @DataSource("commonDB")
    public String add(Integer id, Model model) {
        CencesObs vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new CencesObs();
        }
        model.addAttribute("item", vo);
        return "/cencus/cencesobs/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData edit(CencesObs vo) {
        if (vo.getId() != null) {

            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDate.now());

            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData delete(Integer id) {
        baseService.deleteByTime(baseService.selectById(id).getTime());
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    @DataSource("commonDB")
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        CencesObsExample example = new CencesObsExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        CencesObsExample.Criteria criteria = example.createCriteria();
        criteria.andAppIdEqualTo(MyConfig.getEnv());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
    /**
     * @param page
     * @param limit
     * @param star
     * @param end
     * @param appStore
     * @param category 1新用户0老用户2全部
     * @return
     */
    @Login
    @PostMapping("/page")
    @ResponseBody
    public HashMap newlist(String likeBrand, Long page, Integer limit, Integer star, Integer end, String appStore, int category, int type, String sdkKey,HttpSession session) {
        // limit=limit-1;
        HashMap map = new HashMap(1);
        CencesObsExample example = new CencesObsExample();
        CencesObsExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc");
        c.andAppIdEqualTo(MyConfig.getEnv());
        if (star != null && end != null) {
            c.andTimeBetween(star, end);
        }
        if (!TextUtil.isEmpty(sdkKey)) {
            c.andSdkKeyEqualTo(sdkKey);
        }else {
            sdkKey=null;
        }
        if(!TextUtil.isEmpty(likeBrand)){
            c.andBrandLike(likeBrand+"%");
            appStore=null;
        }else {
            likeBrand=null;
            if (!TextUtil.isEmpty(appStore)) {
                c.andBrandEqualTo(appStore);
            }else {
                appStore=null;
            }
        }
        c.andCategoryEqualTo(category);
        if(newRouseService.isRoot(session)){
            c.andSdkKeyNotIn(sdkList);
        }
        map.put("count", baseService.selectCount(example));
        map.put("code", 0);
        List<CencesObs> masters=baseService.selectAll(example);
        CencesObs vo=baseService.sumData(star,end,appStore,category,type,sdkKey,likeBrand);
        if(null!=vo){
            vo.setTime(-1);
            masters.add(vo);
        }
        map.put("data", masters);
        return map;
    }

    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen( String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN));
        return "success";
    }
}
